#include "ShoppingCart.h"
#include <iostream>

std::string GetTimeStr(time_t time)
{
    tm *m = localtime(&time); 
    char s[1024] = {0};
    sprintf(s,"%d年%d月%d日  %d:%d:%d\n",m->tm_year + 1900,m->tm_mon + 1,m->tm_mday,m->tm_hour,m->tm_min,m->tm_sec);
    return std::string(s);
}

ShoppingCart::ShoppingCart()
{
    
}

ShoppingCart::~ShoppingCart()
{
    while (Cart.IsEmpty() == false) 
    {
        Product* prod = (Product*)*Cart.GetFront();
        delete prod;
        Cart.Pop();
    }
    while (Goods.IsEmpty() == false) 
    {
        Product* prod = (Product*)*Goods.GetTop();
        delete prod;
        Goods.Pop();
    }
}

void ShoppingCart::AddProduct()
{
    Product *c = new Product(" ");
    std::cout << "请输入商品名称：";
    std::cin >> c->name;

    std::cout << "请输入商品价格: ";
    std::cin >> c->price;
    std::cout<<std::endl;

    std::cout<<"成功添加商品 |"<<c->name<<"| 到购物车"<<std::endl;
    Cart.Push(c);  // 将商品添加到购物车队列
}

void ShoppingCart::RemoveProduct()
{
    std::cout << "请输入要删除的商品名称：";
    std::string name;
    std::cin >> name;
    std::cout<<std::endl;

    LinkQueue temp;
    while (Cart.IsEmpty() == false)
    {
        Product* prod = (Product*)*Cart.GetFront();
        Cart.Pop();
        if(prod->name == name)
        {
            Goods.Push(prod);  // 将商品添加到回收站商品队列
            std::cout << "商品 |" << prod->name << "| 已删除。" << std::endl;
        }
        else
        {
            temp.Push(prod);  // 将商品添加到临时队列
        }
    }
    while (temp.IsEmpty() == false)
    {
        Cart.Push(*temp.GetFront());
        temp.Pop();
    }
}

void ShoppingCart::UndoDeletion()
{
    std::cout << "请输入要撤销删除的商品名称：";
    std::string name;
    std::cin >> name;
    std::cout<<std::endl;

    LinkStack temp;

    while (Goods.IsEmpty() == false)
    {
        Product* prod = (Product*)*Goods.GetTop();
        Goods.Pop();

        if(prod->name == name)
        {
            Cart.Push(prod);  // 将商品添加到购物车队列
            std::cout << "商品 |" << prod->name << "| 已撤销删除。" << std::endl;
            break;
        }
        temp.Push(prod);  // 将商品添加到临时队列
    }

    while(temp.IsEmpty() == false)
    {
        Product* prod = (Product*)*temp.GetTop();
        temp.Pop();
        Goods.Push(prod);  // 将商品添加到临时队列
    }
}

void ShoppingCart::ShowCart()
{
    LinkQueue temp;

    if(Cart.IsEmpty() == false)
    {
        std::cout << "购物车中的商品有：" << std::endl;
    }
    while (Cart.IsEmpty() == false)
    {
        temp.Push(*Cart.GetFront());
        Cart.Pop();
    }
    while (temp.IsEmpty() == false)
    {
        Product* prod = (Product*)*temp.GetFront();

        prod->purchaseTime = time(nullptr);  // 获取当前时间
        std::cout << "商品名称：" << prod->name << "，商品价格：" << prod->price << "元| " <<"在"<<GetTimeStr(prod->purchaseTime)<<"被添加进购物车"<<std::endl;
        std::cout << std::endl;
        Cart.Push(prod);  // 将商品添加到购物车队列
        temp.Pop();  // 将商品从临时队列中移除
    }

    LinkStack temp2;
    if(Goods.IsEmpty() == false)
    {
        std::cout << "回收站中的商品有：" << std::endl;
    }
    while (Goods.IsEmpty() == false)
    {
        temp2.Push(*Goods.GetTop());
        Goods.Pop();
    }
    while (temp2.IsEmpty() == false)
    {
        Product* prod = (Product*)*temp2.GetTop();

        prod->deletTime = time(nullptr);  // 获取当前时间
        std::cout << "商品名称：" << prod->name << "，商品价格：" << prod->price << "元| " <<"在"<<GetTimeStr(prod->deletTime)<<"被删除"<< std::endl;
        std::cout << std::endl;
        Goods.Push(prod);  // 将商品添加到商品队列
        temp2.Pop();
    }
}